package com.imooc.ecommerce.stream;

import com.alibaba.fastjson.JSON;
import com.imooc.ecommerce.vo.QinyiMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.support.MessageBuilder;

/**
 * 使用默认的通信信道实现消息的发送
 * @author zhangshao
 * @since 2023/12/10 22:43
 */
@Slf4j
@EnableBinding(Source.class)
public class DefaultSendService {

    @Autowired
    private Source source;

    /**
     * 使用默认的输出信道发送消息
     * @param message
     */
    public void sendMessage(QinyiMessage message){
        String _message = JSON.toJSONString(message);
        log.info("in DefaultSendMessage:[{}]",_message);

        // Spring Messaging, 统一消息的编程模型,是Stream 组件的重要组成部分之一
        source.output().send(MessageBuilder.withPayload(_message).build());
    }
}
